package cn.hebutrun.oulabot.quartz;

import lombok.RequiredArgsConstructor;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;

/** 实现Job注入Spring管理的Bean */
@Component
@RequiredArgsConstructor
public class CustomJobFactory extends SpringBeanJobFactory {

  private final AutowireCapableBeanFactory beanFactory;

  @Override
  @Nonnull
  protected Object createJobInstance(@Nonnull TriggerFiredBundle bundle) throws Exception {
    Object jobInstance = super.createJobInstance(bundle);
    beanFactory.autowireBean(jobInstance);
    return jobInstance;
  }
}
